package cn.edu.zut.score.controller.admin;

import cn.edu.zut.score.core.constant.RoleEnum;
import cn.edu.zut.score.core.domain.PageResponse;
import cn.edu.zut.score.core.domain.RestResponse;
import cn.edu.zut.score.core.dto.request.UserPageRequest;
import cn.edu.zut.score.core.dto.response.UserResponse;
import cn.edu.zut.score.core.entity.User;
import cn.edu.zut.score.core.util.MDUtil;
import cn.edu.zut.score.service.IUserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zby
 * @date 2021/06/27
 */
@RestController
@RequestMapping("/admin/student")
@ApiModel("学生相关api")
@CrossOrigin
public class AdminStudentController {

    @Autowired
    private IUserService iUserService;

    @ApiOperation("添加学生")
    @PostMapping("")
    public RestResponse<UserResponse> addStudent(@RequestBody User student) {
        student.setRole(RoleEnum.student.getCode());
        student.setPassword(MDUtil.encryption("123456"));
        return iUserService.saveUser(student);
    }

    @ApiOperation("导入学生")
    @PostMapping("/import")
    public RestResponse<Integer> importStudents(MultipartFile file) throws Exception {
        return iUserService.importUsers(file.getInputStream(), RoleEnum.student);
    }

    @ApiOperation("根据Id删除学生")
    @DeleteMapping("/{studentid}")
    public RestResponse<Long> deleteStudent(@PathVariable("studentid") Long studentid) {
        return iUserService.deleteUserById(studentid);
    }

    @ApiOperation("根据Ids删除学生")
    @DeleteMapping("/list")
    public RestResponse<Integer> deleteStudents(@RequestBody List<Long> allStudentIds) {
        return iUserService.deleteUsersById(allStudentIds);
    }

    @ApiOperation("更新学生信息")
    @PutMapping("")
    public RestResponse<UserResponse> updateStudent(@RequestBody User student) {
        student.setRole(RoleEnum.student.getCode());
        return iUserService.saveUser(student);
    }

    @ApiOperation("重置学生密码")
    @PutMapping("/{studentid}")
    public RestResponse<UserResponse> resetPassword(@PathVariable("studentid") Long studentid) {
        User student = new User();
        student.setId(studentid);
        student.setRole(RoleEnum.student.getCode());
        student.setPassword(MDUtil.encryption("123456"));
        return iUserService.saveUser(student);
    }

    @ApiOperation("根据Id查询学生")
    @GetMapping("/{studentid}")
    public RestResponse<UserResponse> queryStudent(@PathVariable("studentid") Long studentid) {
        return iUserService.getUserById(studentid);
    }

    @ApiOperation("分页查询学生")
    @PostMapping("/list")
    public RestResponse<PageResponse<UserResponse>> queryStudents(@RequestBody UserPageRequest studnetQuery) {
        studnetQuery.setRole(RoleEnum.student.getCode());
        return iUserService.queryUsers(studnetQuery);
    }
}
